﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SaintWinControl
{
    public partial class ucCRUDToolBar : UserControl
    {
        protected CRUDMode currentMode = CRUDMode.None;
        public CRUDMode CurrentMode
        {
            set
            {
                setAllBtnDisable();
                switch ((int)value)
                {
                    case (int)CRUDMode.None:
                    case (int)CRUDMode.Cancel:
                        noneMode();
                        break;
                    case (int)CRUDMode.Insert:
                    case (int)CRUDMode.Update:
                        insUpdMode();
                        ucBtnExecute.ButtonnMode = (value == CRUDMode.Insert)
                            ? CRUDMode.InsertExecute : CRUDMode.UpdateExecute;
                        break;
                    case (int)CRUDMode.Delete:
                        break;
                    case (int)CRUDMode.Query:
                        queryMode();
                        ucBtnExecute.ButtonnMode = CRUDMode.QueryExecute;
                        break;
                    case (int)CRUDMode.InsertExecute:
                        insUpdMode();
                        break;
                    case (int)CRUDMode.QueryAfter:
                        queryAfterMode();
                        break;
                }
                currentMode = value;   
            }
            get
            {
                return currentMode;
            }
        }

        public event EventHandler ToolBarClick = null;

        public ucCRUDToolBar()
        {
            InitializeComponent();          
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            ucButton btn = sender as ucButton;
            CurrentMode = btn.ButtonnMode;
            if(ToolBarClick != null)
            {
                ToolBarClick(sender,e);
            }
        }

        protected void setAllBtnDisable()
        {
            ucBtnQuery.ButtonnMode = CRUDMode.Query;
            foreach (Control cnt in this.Controls)
            {
                if (cnt.GetType().Name.Equals("ucButton"))
                {
                    cnt.Enabled = false;
                }
            }
        }

        protected void noneMode()
        {
            ucBtnInsert.Enabled = true;
            ucBtnQuery.Enabled = true;
            ucBtnExit.Enabled = true;
        }

        protected void insUpdMode()
        {
            ucBtnExecute.Enabled = true;
            ucBtnCancel.Enabled = true;
        }

        protected void queryMode()
        {
            ucBtnCancel.Enabled = true;
            ucBtnExecute.Enabled = true;
        }

        protected void queryAfterMode()
        {
            queryMode();
            ucBtnUpdate.Enabled = true;
            ucBtnDelete.Enabled = true;
        }

    }
}
